package com.etc.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.CommonMessage;
import com.etc.util.PageData;

@Controller
@RequestMapping(value="api")
public class UserController {
	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping(value = "users", method = RequestMethod.GET)
	@ResponseBody
	public PageData<User> showUsers(int page,int limit) {
		PageData<User> pdu = userService.getAllUser(page,limit);
		pdu.setCode(0);
		pdu.setMsg("成功");
		return pdu;
	}
	@RequestMapping(value="index")
	public String index() {
		
		return "layuitables";
	}
	@RequestMapping(value="index2")
	public String index2() {
		
		return "layuitables2";
	}
	
	@RequestMapping(value = "users2", method = RequestMethod.GET)
	@ResponseBody
	public PageData<User> showUsers2(int page,int limit,String content) {
		if(content==null) {
			content= "";
		}
		PageData<User> pdu = userService.getUserByPage(page, limit, content);
		pdu.setCode(0);
		pdu.setMsg("成功");
		return pdu;
	}
	@RequestMapping(value="users/{userId}",method = RequestMethod.DELETE)
	@ResponseBody
	public CommonMessage deluser(@PathVariable(value="userId") int userId) {
		boolean flag=userService.delete(userId);
		CommonMessage com = new CommonMessage();
		com.setMsg(flag?"删除成功":"删除失败");
		return com;
	}
	@RequestMapping(value="users",method=RequestMethod.PUT)
	@ResponseBody
	public CommonMessage edituser(@RequestBody User user) {
		boolean flag =userService.updateUser(user);
		CommonMessage com = new CommonMessage();
		com.setMsg(flag?"修改成功":"修改失败");
		return com;
	}
	
	@RequestMapping(value="users",method=RequestMethod.POST)
	@ResponseBody
	public CommonMessage adduser(User user) {
		boolean flag =userService.saveUser(user);
		CommonMessage com = new CommonMessage();
		com.setMsg(flag?"新增成功":"新增失败");
		return com;
	}
	

}
